#include "compile.hpp"
#include "runner.hpp"
#include "compile_run.hpp"
#include "../comm/httplib.h"
#include <jsoncpp/json/json.h>

using namespace ns_compiler;
using namespace ns_runner;
using namespace ns_compile_run;
using namespace httplib;

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        std::cout << "Usage:\n\t" << argv[0] << " [port]" << std::endl;
        return 1;
    }

    int port = std::stoi(argv[1]);

    Server svr;

    //// 设置 web 根目录
    // svr.set_base_dir("./wwwroot");

    // 设置用 Post 请求时，该怎么执行
    svr.Post("/CompileAndRun", [](const Request& req, Response& resp){
        std::string out_json;
        std::string in_json = req.body;
        if(!in_json.empty())
        {
            CompileAndRun::Start(in_json, &out_json);
            resp.set_content(out_json, "application/json;charset=UTF-8");
        }
    });

    svr.listen("0.0.0.0", port);

    return 0;
}